#include <gtk/gtk.h>   
#include <gst/gst.h>   
#include <signal.h>   
#include <string.h>   
#include <gst/interfaces/xoverlay.h>
#include <gdk/gdkx.h>


#include "config.h"
#include "message_handler.h"
#include "four_gui.h"
#include "big_screen_gui.h"
#include "sys_time.h"
#include "sys_log.h"
#include "sys_xtest.h"
#include "printer_libgd.h"

//language chinese
#define PACKAGE "base" 
#define PACKAGE_LOCALE_DIR "/usr/local/share/locale" 

print_sys_config_s *sys_time_config = NULL;
GMainLoop *loop = NULL;


static gboolean xtest_key()
{
	Display *dsp=dspopen();
	//move(dsp,500,500);
  	//buttonpress(dsp,1);
  	//buttonrelease(dsp,1);
	//use  XK_Control_L                     0xffe3  /* Left control */
   	move(dsp,2000,0); 	
  	presskey(dsp,65507);
  	releasekey(dsp,65507);

  	dspclose(dsp);
	SYS_LOG("xtest_in");
	return TRUE;
}


int main(int argc,char *argv[])  
{  
	GtkWidget *window = NULL; 
	bindtextdomain(PACKAGE,PACKAGE_LOCALE_DIR);
	textdomain(PACKAGE);	

	XInitThreads();
    gtk_init(NULL,NULL);  

	sys_time_config = (print_sys_config_s*)malloc(sizeof(print_sys_config_s));
	memset(sys_time_config,0,sizeof(sys_config_s));

 	//sys config
	read_text_config_file(CLIENT_SYS_CONFIG_FILE,CLIENT_SYS_HEART_BEAT,&(sys_time_config->sys_config));
	char* ip_addr = NULL;
	char* mac_addr = NULL;

	get_ip_mac_address(&ip_addr,&mac_addr);
	g_stpcpy(sys_time_config->sys_config.mac_addr,mac_addr);
  	g_stpcpy(sys_time_config->sys_config.eth0_mac_addr,ip_addr);
    g_stpcpy(sys_time_config->sys_config.log_name,ip_addr);

    if (NULL != ip_addr) free(ip_addr);
    if (NULL != mac_addr) free(mac_addr);

   	//open log system
	sys_log_open(sys_time_config->sys_config.log_name);

	sprintf(sys_time_config->sys_config.image_wifi_on,".%s/%s",RES_PATH,"wifi_on.png");
	sprintf(sys_time_config->sys_config.main_bg,".%s/%s",RES_PATH,"main_bg.png");
	sprintf(sys_time_config->sys_config.frame_bg,".%s/%s",RES_PATH,"frame_bg.png");
	sprintf(sys_time_config->sys_config.image_bg,".%s/%s",RES_PATH,"image_bg.png");
	sprintf(sys_time_config->sys_config.image_logo,".%s/%s",AD_PATH,"logo.png");
	sprintf(sys_time_config->sys_config.image_qr,".%s/%s",AD_PATH,"qr.png");
   	sprintf(sys_time_config->sys_config.image_bottom,".%s/%s",AD_PATH,"bottom.jpg");
    sprintf(sys_time_config->sys_config.text_font_path,".%s/%s",RES_PATH,"fzjl.ttf");
    sprintf(sys_time_config->sys_config.printer_error_pic,".%s/%s",RES_PATH,"print_error.png");
    sprintf(sys_time_config->sys_config.mp3_playlist,".%s/%s",RES_PATH,"mp3.list");
    sprintf(sys_time_config->sys_config.image_sound,".%s/%s",RES_PATH,"sound.png");
  	save_text_config_file(CLIENT_SYS_CONFIG_FILE,&(sys_time_config->sys_config));
#if 0
	sprintf(sys_time_config->sys_config.right_fixed_label_1,"使用说明");
	sprintf(sys_time_config->sys_config.right_fixed_label_2,"照片新玩法");
	sprintf(sys_time_config->sys_config.right_label_1,"关注上面的微信");
	sprintf(sys_time_config->sys_config.right_label_2,"发一张照片");
	sprintf(sys_time_config->sys_config.right_label_3,"输入微信验证码");
	sprintf(sys_time_config->sys_config.right_label_4,"马上制作LOMO照片");
	sprintf(sys_time_config->sys_config.device_id,"machine 001");
	sprintf(sys_time_config->sys_config.heart_beat_url,"http://112.124.103.153/webprint/print.php?hash=");
	sprintf(sys_time_config->sys_config.client_http_hash,"test1");	
	save_text_config_file(CLIENT_SYS_CONFIG_FILE,&(sys_time_config->sys_config));
#endif
	//jpg init
	jpg_player_read_filelist();

	//gst init
	gst_init(NULL,NULL);
  

	loop=g_main_loop_new(NULL,FALSE);
  
	player_read_filelist();
	player_play_mp4_set_loop(loop);

	//set window size
	GdkScreen* screen;
	screen = gdk_screen_get_default();
	sys_time_config->screen_w= gdk_screen_get_width(screen);
	sys_time_config->screen_h = gdk_screen_get_height(screen);
	SYS_LOG("w[%d]h[%d]",sys_time_config->screen_w,sys_time_config->screen_h);

//start mp3 

	FILE *fp;
	fp = popen("sudo killall mp3blaster", "r");
	pclose(fp);
	if(NULL == g_strrstr(sys_time_config->sys_config.screen_module,"mp4")) {

		printf("start mp3\n");
		mp3_read_filelist(sys_time_config);
//		fp = popen("gnome-terminal --geometry=0x0+0+0 --hide-menubar -x mp3blaster -a ./res/mp3.list -R &", "r");
		fp = popen("gnome-terminal -x mp3blaster -a ./res/mp3.list -R &", "r");
		pclose(fp);
	}
	sleep(1);

//mp3 end

	//build window
	if(NULL == g_strrstr(sys_time_config->sys_config.screen_module,"big_screen")) {
		sys_time_config->window = four_gui_builder(sys_time_config);
	} else {
		sys_time_config->window = big_screen_gui_builder(sys_time_config);
	}
	


	//clear print jobs
	printing_cancle_jobs();
	
	//heart beat
	server_heartbeat_message(sys_time_config);
	
//    image_generateImg(char * psrc,char * text,char * logo_path,int px,int py,int p_w,int p_h,int type,char * font)
//			xtest_mouse();


	//block screen-saver
	xtest_key();
	g_timeout_add(60000*5,(GSourceFunc)xtest_key,NULL);// 10 mins kick	

    	gtk_main();  

    return 0;  
}

